import { ref,computed } from "vue";
import { defineStore } from "pinia";
import GoodsInfo from "@/pages/goods/GoodsInfo.vue";

export const useCartStore =defineStore('cart',()=>{
    let cartList=ref([])
    let buyList=ref([])
    const addCart = (goodsinfo)=>{
        var flag=false
        cartList.value.some((item)=>{
            if(item.id==goodsinfo.id){
                item.count += parseInt(goodsinfo.count)
                flag = true
                return true
            }
        })
        if(!flag){
            cartList.value.push(goodsinfo)
        }
    }
    const delCart=(id)=>{
        const index=cartList.value.findIndex((item)=>{
            if(item.id==id){
                return true
            }
        })
        cartList.value.splice(index,1)
    }
    const setCartSelected = (itemid) => {
        const item=cartList.value.find((v)=>{
            return v.id==itemid
        })
        item.selected=true
    }
    const setCartUnselected = (itemid) => {
        const item=cartList.value.find((v)=>{
            return v.id==itemid
        })
        item.delected=false
    }
    return{
        addCart,
        delCart,
        setCartSelected,
        setCartUnselected,
    }
})